from bs4 import BeautifulSoup

if __name__ == '__main__':
    # 从文件中读取内容，并保存到 text 变量中
    f = open("The Dormouse's story.html", mode="r", encoding="utf-8")
    text = f.read()
    f.close()

    # 将文本内容进行解析
    soup = BeautifulSoup(text, "html.parser")

    # CSS 选择器 元素标签选择器
    # result = soup.select("title")
    # print(result)

    # #
    # result = soup.select("p:nth-of-type(3)")
    # print(result)

    # # 子代选择器
    # result = soup.select("body a")
    # print(result)
    # # 直接子代选择器
    # result = soup.select("head > title")
    # print(result)

    # result = soup.select("p > a:nth-of-type(2)")
    # print(result)
    #
    # result = soup.select("p > #link1")
    # print(result)

    # # 类选择器
    # result = soup.select(".sister")
    # print(result)
    #
    # result = soup.select("[class=sister]")
    # print(result)

    # 兄弟选择器
    result = soup.select("#link1 ~ .sister")
    print(result)
    # 相邻相抵选择器
    result = soup.select("#link1 + .sister")
    print(result)
